首页
首页

python 定义函数来实现输出kunbernetes svc配置文件

紧接着上一篇python 定义函数来实现输出kunbernetes yaml配置文件文章

这篇文章来贴上 python自定义函数来输出service.yaml方法

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
def create_svc_yaml(post_on,rc_name,containes,pod_name,namespace,pod_version):
nodePort = int(Port_all.objects.latest('id').wport) + 1 # 此处为从数据库获取最新的端口并+1
json_content = read_yaml(svc_path)
json_content['metadata']['labels']['app'] = pod_name
json_content['metadata']['namespace'] = namespace
json_content['metadata']['name'] = pod_name
json_content['spec']['selector'] = pod_name
i = 0
while i < int(len(post_on)):
for m in post_on:
print m
json_content['spec']['ports'][i]['name'] = rc_name[0:4] + '-tcp' # 由于有字数限制,所有取前四个字符+‘-tcp’来定义post_name
json_content['spec']['ports'][i]['nodePort'] = nodePort + m
json_content['spec']['ports'][i]['targetPort'] = int(containes['port'+str(m)])
json_content['spec']['ports'][i]['port'] = nodePort + m
i += 1
yaml_content = dump(json_content,default_flow_style=False)
# 将yaml输出到文件
save_svc_yaml_path = project_yaml_path + pod_name +'/' + pod_version + '/' #定义 保存service yaml的路径
isExists = os.path.exists(save_svc_yaml_path) # 判断路径是否存在
print save_svc_yaml_path
if not isExists:
try:
os.makedirs(save_svc_yaml_path) # 创建路径
write_yaml = open(save_svc_yaml_path + '//' + str(pod_name) +'-svc.yaml','w')
write_yaml.write(yaml_content)
write_yaml.close()
return os.path.isfile(save_svc_yaml_path + '//' + str(pod_name) +'-svc.yaml')
except Exception as e:
return e
else:
if os.path.isfile(save_svc_yaml_path + '//' + str(pod_name) +'-svc.yaml'):
return 'File exists, Please contact Administrator QQ:zhizhebuyan!!!'
else:
write_yaml = open(save_svc_yaml_path + '//' + str(pod_name) +'-svc.yaml','w')
write_yaml.write(yaml_content)
write_yaml.close()
return os.path.isfile(save_svc_yaml_path + '//' + str(pod_name) +'-svc.yaml')

参数介绍:

post_on —- 是一个list 里面存储的是pod中容器的位置

rc_name—-项目别名

containers —-是一个json 例子:{‘containes_name0’: ‘whpt’,’port0’:8088}

pod_name —-表示唯一项目名不可更改

namespace —-命名空间

pod_version—-版本号,随着项目的滚动更新而变化。

这边文章就到这了,感谢你浪费时间观看此篇文章

支持一下
扫一扫,我会更有动力更新
  • 微信扫一扫
  • 支付宝扫一扫